package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.GreFileLogic;
import cn.topevery.garden.entity.dto.GreFileDTO;
import cn.topevery.garden.entity.po.GreFile;
import cn.topevery.garden.entity.query.GreFileQuery;
import cn.topevery.garden.entity.vo.GreFileVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 绿地/行道树/古树名木  附件表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "绿地/行道树/古树名木  附件表")
@RestController
@RequestMapping("/greFile")
public class GreFileController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    GreFileLogic greFileLogic;


    /**
     * 获取绿地/行道树/古树名木  附件表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取绿地/行道树/古树名木  附件表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<GreFileVO>> listPage(@RequestBody GreFileQuery query) {
        return JsonResult.ok(greFileLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 绿地/行道树/古树名木  附件表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody GreFileDTO dto) {
        greFileLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, GreFile.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<GreFileVO> getInfoById(@RequestParam Integer id) {
        GreFile entity = greFileLogic.getEntity(id);
        GreFileVO vo= mapper.map(entity, GreFileVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        greFileLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出绿地/行道树/古树名木  附件表信息")
    @GetMapping("/export")
    public void export(GreFileQuery query, HttpServletResponse response) {
        List<GreFileVO> list = greFileLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "绿地/行道树/古树名木  附件表", "sheet1", GreFileVO.class, "绿地/行道树/古树名木  附件表", response);
    }

    @GetMapping(value = "/getList")
    public JsonResult<?> getList(Integer mainId, Integer dataType) {
        List<GreFile> list = greFileLogic.getListByMainId(mainId, dataType);
        return JsonResult.ok(list);
    }

}
